"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 故障&可靠性测试
Case Name   : DN主+DN备故障	mv gaussdb，kill -9杀掉圈内dn主备进程
Create At   : 2020/07/02
@zou_jialiang0505328114
Description :
    步骤 1.启动数据库
    步骤 2.运行tpcc程序
    步骤 3.移除dn1目录，kill -9掉主备
    步骤 4.查看数据库状态
    步骤 5.运行tpcc程序
    步骤 6.重启数据库
    步骤 7.查看数据库状态
Expect      :
    步骤 1.启动数据库正常
    步骤 2.运行tpcc程序正常
    步骤 3.移除dn1目录，kill -9掉主备成功
    步骤 4.主备stop
    步骤 5.运行tpcc程序，拒绝连接
    步骤 6.重启数据库成功
    步骤 7.主备normal
History     :
    modified: 2020/12/09  modified by @wan005,恢复配置文件后 启动数据库概率性
    提示Degrade，在启动前增加等待，并修改断言
    modified: 2021/1/12 modified by @peilinqian，恢复配置文件后，启动数据库后，
    查询主备状态可能备1状态为Standby Need repair(Disconnected)，删除启动成功断言
    等待主备同步后，再次进行状态为TRUE断言
"""
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import RestartDbCluster
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class Deletaduit(unittest.TestCase):
    sh_primy = CommonSH('PrimaryDbUser')
    sh_standby = CommonSH('Standby1DbUser')
    nodes_tuple = ('PrimaryDbUser', 'Standby1DbUser', 'Standby2DbUser')

    @RestartDbCluster(*nodes_tuple)
    def setUp(self):
        self.log = Logger()
        self.log.info(
            '-------Opengauss_Reliability_Dbsys_Case010.py start-------')
        self.primary_dbuser = Node(node='PrimaryDbUser')
        self.primary_root = Node(node='PrimaryRoot')
        self.standby1_root = Node(node='Standby1Root')
        self.standby1_dbuser = Node(node='Standby1DbUser')
        self.com = Common()
        self.TPCC_PATH = macro.TPCC_PATH

    def test_startdb(self):
        self.log.info('-------运行tpcc程序-------')
        result = self.com.startTPCC(self.primary_root, self.TPCC_PATH,
                                    f'. runBenchmark.sh'
                                    f' {macro.TPCC_PROPS_FILENAME}')
        self.log.info(result)
        self.assertTrue(result.find('Session End') > -1)

        # 移除gaussdb
        self.log.info('-------移除gaussdb-------')
        mvcmd = f"mv {macro.DB_INSTANCE_PATH} " \
            f"{macro.DB_INSTANCE_PATH + '.baktest'} "
        self.standby1_root.sh(mvcmd)
        self.primary_root.sh(mvcmd)
        self.log.info(mvcmd)

        self.log.info('-------kill -9掉主备-------')
        msg = self.com.kill_pid(self.standby1_root, 9)
        self.assertEqual(msg, '')
        msg = self.com.kill_pid(self.primary_root, 9)
        self.assertEqual(msg, '')

        self.log.info('-------查看数据库状态-------')
        status = self.sh_primy.getDbClusterStatus("status")
        self.log.info(status)
        self.assertFalse(status)

        self.log.info('-------运行tpcc程序-------')
        result = self.com.startTPCC(self.primary_root, self.TPCC_PATH,
                                    f'. runBenchmark.sh'
                                    f' {macro.TPCC_PROPS_FILENAME}')
        self.log.info(result)
        self.assertTrue(result.find('refused.') > -1)

        self.log.info('-------恢复dn1目录-------')
        mvcmd1 = f"mv {macro.DB_INSTANCE_PATH + '.baktest'} " \
            f"{macro.DB_INSTANCE_PATH}"
        self.primary_root.sh(mvcmd1)
        self.standby1_root.sh(mvcmd1)
        self.log.info(mvcmd1)

        self.log.info('------------重启数据库-----------')
        time.sleep(25)
        self.sh_primy.startDbCluster(True)

        self.log.info('------------等待主备同步-----------')
        self.sh_primy.check_location_consistency('primary', 3)

        self.log.info('-------查看数据库状态-------')
        status = self.sh_primy.getDbClusterStatus("status")
        self.log.info(status)
        self.assertTrue(status)

        self.log.info('-------运行tpcc程序-------')
        result = self.com.startTPCC(self.primary_root, self.TPCC_PATH,
                                    f'. runBenchmark.sh'
                                    f' {macro.TPCC_PROPS_FILENAME}')
        self.log.info(result)
        self.assertTrue(result.find('Session End') > -1)

    def tearDown(self):
        self.log.info(
            "-------this is tearDown-------")
        db_status = self.sh_primy.getDbClusterStatus('status')
        if not db_status:
            self.log.info('-------恢复数据库目录-------')
            mvcmd1 = f"mv {macro.DB_INSTANCE_PATH + '.baktest'}" \
                f" {macro.DB_INSTANCE_PATH}"
            self.standby1_root.sh(mvcmd1)
            self.primary_root.sh(mvcmd1)
            self.log.info(mvcmd1)
            self.sh_primy.startDbCluster()
        else:
            pass
